package com.AC.ACHuanJu;

import org.junit.Test;

/**
 * 水仙花数
 */
public class test1 {

    /**
     * 找到比输入的整数大的下一个水仙花数
     * @param n int整型 输入的整数
     * @return long长整型
     */
    public long nextNarcissisticNumber (int n) {
        for(int i = n + 1; i<Integer.MAX_VALUE; i++){
            if(isNarcissisticNumber((i)))
                return i;
        }
        return -1;
    }

    public boolean isNarcissisticNumber(int n){
        int len = String.valueOf(n).length();
        int target = n;
        long res = 0;
        for(int i = 0; i<len; i++){
            res += QuickPow(n % 10, len);
            n /= 10;
        }
        return res == target;
    }

    public long QuickPow(int x, int n){
        long res = 1;

        long x_contribute = x;

        while(n>0){
            if(n%2 == 1){
                res *= x_contribute;
            }
            x_contribute *= x_contribute;
            n /= 2;
        }
        return res;
    }

    @Test
    public void test(){
        System.out.println(nextNarcissisticNumber(2));
    }
}
